package org.springframework.ai.mcp.samples.client;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import java.util.Map;

@RestController
public class AgentController {

    private final ChatClient chatClient;

    private final String systemPrompt;

    public AgentController(ChatClient chatClient, String systemPrompt) {
        this.chatClient = chatClient;
        this.systemPrompt = systemPrompt;
    }

    @GetMapping(value = "/api")
    public String streamResponse(@RequestParam String username) {
        String string = chatClient.prompt(username)
                .advisors(new SimpleLoggerAdvisor())
                .call().content();
        return string;
    }

    @PostMapping(value = "/chat")
    public Mono<Map<String, String>> chat(@RequestBody Map<String, String> request) {
        String message = request.get("message");
        if (message == null || message.trim().isEmpty()) {
            return Mono.just(Map.of("response", "请输入消息"));
        }

        return Mono.fromCallable(() -> {
            String response = chatClient.prompt().system(systemPrompt).user(message)
                    .advisors(new SimpleLoggerAdvisor())
                    .call()
                    .content();
            return Map.of("response", response);
        });
    }
}
